#include "examples.h"

#define MC_LOGISTIC_R_DIR C_TMP_DIR "mcLogisticR\\"


void runmcLogisticR(){
	int m,n,k,state,i;
	double *A, *xorin, *noise, *y, rho;
	OPTS opt;
	double c1,c2;
	stopWatch sw;
	FUNVAL result1,result2,result3;
	m=n=100;
	k=10;
	A= (double *) malloc( sizeof(double) * m* n);
	xorin = (double *) malloc(sizeof(double) * n*k);
	noise = (double *) malloc(sizeof(double) * m*k);
	y = (double *) malloc(sizeof(double) * m*k);

	state = 2344;
	acml_randseed(state);
	acml_randn(A,m*n,0.0,1.0);

	state = 13255;
	acml_randseed(state);
	acml_randn(xorin,n*k,0.0,1.0);

	state = 333;
	acml_randseed(state);
	acml_randn(noise,m*k,0.0,1.0);
	dcopy(m*k,noise,1,y,1);

	for(i=0;i<m*k;i++)
		y[i] = (y[i]>0)?1:-1;

	fprintM(MC_LOGISTIC_R_DIR "A.matrix",A,m,n);
	fprintM(MC_LOGISTIC_R_DIR  "y.matrix",y,m,k);
	rho = 0.1l;
	memset(A,0,m*n*sizeof(double));
	memset(y,0,m*k*sizeof(double));
	fscanM(MC_LOGISTIC_R_DIR "A.matrix",A,m,n);
	fscanM(MC_LOGISTIC_R_DIR "y.matrix",y,m,k);

	initOPTS(&opt,m,n,0);
	opt.mc_k = k;
	opt.q = 2.0;
	opt.init = 2;
	opt.maxIter = 100;
	opt.nFlag = 0;
	opt.rsL2 = 0;
	opt.tFlag = 5;
	opt.rFlag = 1;
	c2 = 0.4l;

//	initNormalization(&opt,A);

	opt.mFlag = 0;
	opt.lFlag = 0;
	startTimer(&sw);
	result1 = mcLogisticR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(MC_LOGISTIC_R_DIR "x1.matrix",result1.x,n,k);
	fprintM(MC_LOGISTIC_R_DIR "funVal1.list",result1.funVal,opt.maxIter,1);
	fprintM(MC_LOGISTIC_R_DIR "lVal1.list",result1.ValueL,opt.maxIter,1);

	opt.maxIter = 200;
	opt.tFlag = 2;
	opt.tol = result1.funVal[result1.totIter-1];

	startTimer(&sw);
	opt.mFlag = 1;
	opt.lFlag = 0;
	result2 = mcLogisticR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(MC_LOGISTIC_R_DIR "x2.matrix",result2.x,n,k);
	fprintM(MC_LOGISTIC_R_DIR "funVal2.list",result2.funVal,result2.totIter,1);
	fprintM(MC_LOGISTIC_R_DIR "lVal2.list",result2.ValueL,opt.maxIter,1);
	free(result2.x);
	free(result2.funVal);
	free(result2.ValueL);

	startTimer(&sw);
	opt.mFlag = 1;
	opt.lFlag = 1;
	result3 = mcLogisticR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(MC_LOGISTIC_R_DIR "x3.matrix",result3.x,n,k);
	fprintM(MC_LOGISTIC_R_DIR "funVal3.list",result3.funVal,result3.totIter,1);
	fprintM(MC_LOGISTIC_R_DIR "lVal3.list",result3.ValueL,opt.maxIter,1);
	free(result3.x);
	free(result3.funVal);
	free(result3.ValueL);

	free(result1.x);
	free(result1.funVal);
	free(result1.ValueL);


}

